#########################################################################
#
# Copyright 2008 Gavin Bong
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
#########################################################################

from __future__ import with_statement # 2.5 only
import random
import thread
from soaplib_handler import DjangoSoapApp, soapmethod, soap_types, wrapper

class Returning(wrapper.ClassSerializer):
    soap_members = {}
    namespace = 'http://raverun.com/'

    def __init__(self):
       wrapper.ClassSerializer.__init__(self)

    CallId = 8
    ResellerCallResult = 1

class CloneService(DjangoSoapApp):

    __tns__ = 'http://www.jajah.com/'

    @soapmethod(soap_types.String, soap_types.String, soap_types.String, soap_types.String, soap_types.String, soap_types.Array(soap_types.String), soap_types.String, soap_types.String, _returns=Returning)
    def ResellerCall(self,apiUserName,apiPassword,payingUserName,payingPassword,FromNumber,ToNumbers,privateMode,CallBackUrl):
         global lock
         global currentCallId

         results = Returning()

         results.soap_members['ResellerCallResult'] = soap_types.Integer
         results.soap_members['CallId'] = soap_types.Integer

         r = random.randrange(-14,10,1)

         if r == 0:
             r = r + 1


         if r >= 1:
             with lock:
                 currentCallId = currentCallId + 1
                 results.CallId = currentCallId
         else:
             results.CallId = 0

         results.ResellerCallResult = r

         return results

lock = thread.allocate_lock()
currentCallId = 3000
clone_service = CloneService()
